<?php
namespace app\common\service;

use think\Model;
use app\common\model\Visiting;

class VisitingService extends Model
{

    /**
     * 客户拜访记录
     * @param Visiting $visitingModel
     * @param int $sid
     * @param int $uid
     * @param int $cid
     * @param int $p
     */
    public function getVisitingList(Visiting $visitingModel,$sid,$uid,$cid,$p,$pageSize=5){
        $where = [
            'store_id' => $sid,
            'uid'=>$cid
        ];
        if($uid > 0){
            //uid=0,后台查询，显示所有顾问对该客户的拜访
            $where['create_uid'] = $uid;
        }
        $list_count = $visitingModel->where($where)->count();
        //分页查询
        $visiting_list = $visitingModel->where($where)->order('vid DESC')->limit(($p-1)*$pageSize,$pageSize)->select();
        if (!empty($visiting_list)){
            $userServer = new UserService();
            foreach($visiting_list as $k=>$v){
                $visiting_list[$k]['addtime']=date('Y-m-d H:i:s',$v['addtime']);
                $visiting_list[$k]['next_time']=date('Y-m-d H:i:s',$v['next_time']);
                $visiting_list[$k]['counselor_name'] = $userServer->getUserNicknameByUID($v['create_uid']);
            }
        }
        return ['content' => $visiting_list,'page' =>['count' =>$list_count]];
    }

    public function getVisitingListNew(Visiting $visitingModel, $sid, $uid, $cid, $student_id, $p, $pageSize=5){
        $where = [
            'store_id' => $sid
        ];
        if($cid > 0){
            $where['uid'] = $cid;
        } else {
            $where['student_id'] = $student_id;
        }

        if($uid > 0){
            //uid=0,后台查询，显示所有顾问对该客户的拜访
            $where['create_uid'] = $uid;
        }
        $list_count = $visitingModel->where($where)->count();
        //分页查询
        $visiting_list = $visitingModel->where($where)->order('vid DESC')->limit(($p-1)*$pageSize,$pageSize)->select();
        if (!empty($visiting_list)){
            $userServer = new UserService();
            foreach($visiting_list as $k=>$v){
                $visiting_list[$k]['addtime']=date('Y-m-d H:i:s',$v['addtime']);
                $visiting_list[$k]['next_time']=date('Y-m-d H:i:s',$v['next_time']);
                $visiting_list[$k]['counselor_name'] = $userServer->getUserNicknameByUID($v['create_uid']);
            }
        }
        return ['content' => $visiting_list,'page' =>['count' =>$list_count]];
    }


    /**
     * 添加客户拜访记录
     * @param Visiting $visitingModel
     * @param int $sid
     * @param int $uid
     * @param int $cid
     */
    public function addVisitingRecord(Visiting $visitingModel,$sid,$uid,$cid,$record,$next_time){

        $data=[
            'store_id'=>$sid,
            'uid'=>$cid,
            'create_uid'=>$uid,
            'record'=>$record,
            'next_time'=>strtotime($next_time),
            'addtime'=>time()
            ];
        $res = $visitingModel->insertGetId($data);
        return $res;
    }

    public function addVisitingRecordNew(Visiting $visitingModel, $sid, $uid, $student_id, $record, $next_time){

        $cid = db('students')->where(['sid' => $sid, 'id' => $student_id])->value('uid');
        // 更新学员拜访记录时间
        db('students')->where(['sid' => $sid, 'id' => $student_id])->setField('visiting_update_time', time());

        $data=[
            'store_id'=>$sid,
            'uid'=> $cid ?? 0,
            'create_uid'=>$uid,
            'record'=>$record,
            'next_time'=>strtotime($next_time),
            'addtime'=>time(),
            'student_id' => $student_id
            ];
        $res = $visitingModel->insertGetId($data);
        return $res;
    }

    /**
     * 获取某个客户被拜访的记录总次数
     * @param $sid
     * @param $uid
     * @return int
     */
    public function getCustomerVisitingCount(Visiting $visitingModel,$sid,$uid){
        return $visitingModel->where(['store_id' => $sid,'uid' => $uid])->count();
    }

    /**
     * 获取某个客户最近拜访时间
     * @param Visiting $visitingModel
     * @param $sid
     * @param $uid
     * @return bool|string
     */
    public function getCustomerVisitingNew(Visiting $visitingModel,$sid,$uid){
        $where = [
            'store_id' => $sid,
            'uid' => $uid,
            'next_time'=>['>',time()],
        ];
        $info = $visitingModel->where($where)->order('next_time desc')->field('next_time')->find();
        if($info['next_time']>0){
            return date('Y-m-d',$info['next_time']);
        }else{
            return '';
        }
    }
}







